﻿using CMS.Models;
using CMS.Windows;
using CommonServiceLocator;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Ioc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CMS.ViewModel
{
    public class RecordViewModel : ViewModelBase
    {

        public RecordViewModel() {
            Records = new RecordProvider().Select();
        }

        private List<Record> records = new List<Record>();
        public List<Record> Records
        {
            get { return records; }
            set
            {
                records = value;
                RaisePropertyChanged();
            }
        }

        public RelayCommand<Record> OpenAddInputCargoWindowCommand
        {
            get
            {
                return new RelayCommand<Record>((record) =>
                {
                    var inputCargoViewModel = ServiceLocator.Current.GetInstance<InputCargoViewModel>();
                    if (inputCargoViewModel == null) return;
                    var cargo = new CargoProvider().Select().FirstOrDefault(item => item.Id == record.CargoId);
                    if(cargo == null) return;
                    inputCargoViewModel.Cargo = cargo;
                    var window = new InputCargoWindow();
                    window.ShowDialog();
                    //刷新数据
                    Records = new RecordProvider().Select();
                });
            }
        }

        /// <summary>
        /// 打开出库对话框
        /// </summary>
        public RelayCommand<Record> OpenAddOutputCargoWindowCommand
        {
            get
            {
                return new RelayCommand<Record>((record) =>
                {
                    var outputCargoViewModel = ServiceLocator.Current.GetInstance<OutputCargoViewModel>();
                    if (outputCargoViewModel == null) return;
                    var cargo = new CargoProvider().Select().FirstOrDefault(item => item.Id == record.CargoId);
                    if (cargo == null) return;
                    outputCargoViewModel.Cargo = cargo;
                    var window = new OutputCargoWindow();
                    window.ShowDialog();
                    //刷新数据
                    Records = new RecordProvider().Select();
                });
            }
        }



    }
}
